package com.scau.mailservice.serviceImpl;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import service.MailService;

@Service
@RabbitListener(queuesToDeclare = @Queue(value = "mail"))
public class MailServiceImpl implements MailService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    public String test() {
        return null;
    }
    @RabbitHandler
    public void sendTextMessages(String[] info) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(info[0]);
        simpleMailMessage.setSubject(info[1]);
        simpleMailMessage.setText(info[2]);
        mailSender.send(simpleMailMessage);
    }
}
